import utils
import time
import concurrent.futures
from statistics import mean, stdev

def test_redfish(hostname, port, username, password, report):
    redfish_obj = utils.create_redfish_obj(hostname, username, password)
    
    # task 2 获取Board
    response, response_time = utils.send_redfish_Getmsg(redfish_obj, '/redfish/v1/Chassis/1/Boards')
    if response.status == 200:
        report["redfish_getBoards"] = response_time
        
    # task 1 获取SystemOverview
    response, response_time = utils.send_redfish_Getmsg(redfish_obj, '/redfish/v1/SystemOverview')
    if response.status == 200:
        report["redfish_SystemOverview"] = response_time
        
    # task 2 获取Board
    # response, response_time = utils.send_redfish_Getmsg(redfish_obj, '/redfish/v1/Chassis/1/Boards')
    # if response.status == 200:
    #     report["redfish_getBoards"] = response_time
        
    # task 3 特殊Redfish接口 风扇
    response, response_time = utils.send_redfish_Getmsg(redfish_obj, '/redfish/v1/Chassis/1/Thermal')
    if response.status == 200:
        report["redfish_getThermal"] = response_time
        
    # task 4 特殊Redfish接口 内存
    response, response_time = utils.send_redfish_Getmsg(redfish_obj, '/redfish/v1/Systems/1/Memory')
    if response.status == 200:
        report["redfish_getMemory"] = response_time
    
    redfish_obj.logout()
    
    return report

def test_redfish_inpress(hostname, port, username, password, report):
    # 设置压力测试参数
    num_request = 100 # 总请求数
    num_threads = 10  # 并发线程数
    request_url = '/redfish/v1/Systems/1' # 请求路径
    response_times = [] # 用于存储响应时间
    status_codes = []   # 用于存储状态码
    
    # 创建线程池
    with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
        # 创建Redfish客户端
        clients = [utils.create_redfish_obj(hostname, username, password) for _ in range(num_threads)]
        clients = [client for client in clients if client is not None]  # 过滤掉创建失败的客户端
        
        # 提交任务
        futures = []
        for i in range(num_threads):
            client = clients[i % len(clients)] # 轮询使用客户端
            futures.append(executor.submit(utils.send_redfish_Getmsg, client, request_url))
            
        # 收集结果
        for future in concurrent.futures.as_completed(futures):
            response, response_time = future.result()
            if response_time is not None and response.status == 200:
                response_times.append(response_time)
                status_codes.append(response.status)
            elif response.status != 200 :
                print("压力测试请求失败")
    
    # 登出所有客户端
    # for client in clients:
        # client.logout()
        
    # 统计结果
    if response_times:
        print(f"平均响应时间：{mean(response_times):.2f} 秒")
        print(f"响应时间标准差：{stdev(response_times):.2f} 秒")     
        print(f"最大响应时间：{max(response_times):.2f} 秒")
        print(f"最小响应时间：{min(response_times):.2f} 秒")
        report["redfish压力测试，10线程共并发100个请求，平均响应时间"] = mean(response_times)
        report["redfish压力测试，10线程共并发100个请求，响应时间标准差"] = stdev(response_times)
        report["redfish压力测试，10线程共并发100个请求，最大响应时间"] = max(response_times)
        report["redfish压力测试，10线程共并发100个请求，最小响应时间"] = min(response_times)
    else:
        print("没有发送成功的请求。")
        
    return report
        
            
            